(function(){
'use strict';
/**
 * @ngdoc function
 * @name sasApp.controller:OmdisplaySysbusstatSysbusstatCtrl
 * @description
 * # OmdisplaySysbusstatSysbusstatCtrl
 * Controller of the sasApp
 */
//输入框控制器failedTradeTable
angular.module('controllers.omdisplay.failedTrade', [])
.controller('failedTradeCtrl', failedTradeinput)
.controller('failedTradeTable', failedTradeTable);
/**
 * [failedTradeinput description]
 * @param  {[type]} $scope              [Angular对象]
 * @param  {[type]} contrastiveAnalysis [业务量对比分析server服务]
 * @param  {[type]} $timeout            [定时器]
 * @param  {[type]} $filter             [过滤器]
 * @param  {[type]} $mdDialog           [提示控件]
 * @param  {[type]} CoreService         [公共服务]
 * @param  {[type]} COREDATA            [公共服务]
 */

function failedTradeinput($scope, contrastiveAnalysis, $timeout, $filter, $mdDialog, CoreService, COREDATA,FailedStatics,contrastiveLogicServer,getChannel,getAgency,failedTradServer,FailedLogic) {
  $scope.param={};
  /**
   * [timeType 账期字段,默认设置为年,所以给code设置为4]
   * @type {String}
   */
  $scope.param.timeType = '1';
  $scope.params={};
  $scope.params=FailedStatics.params;
  $scope.yearitem = {};
  //账期下拉查询
  $scope.timeTypes =COREDATA.TIMETYPES;
  //季度下拉框
  $scope.quarters=COREDATA.QUARTERS;
  $scope.errorInfo = COREDATA.ERRORINFO;
  /**
   * [provinces 页面省份迭代对象,数据来源公共请求]
   * @type {[Object]}
   */
   $scope.provinces = COREDATA.PROVINCES;
  /**
   * [businessLines 页面业务线迭代对象,数据来源公共请求]
   * @type {[Object]}
   */
  $scope.businessLines = [];
  angular.copy(COREDATA.BUSINESSLINE,$scope.businessLines);
  $scope.businessLines.pop();
  $scope.businessLines.pop();
  $scope.returnCodes=FailedStatics.getReturnCode;
  FailedLogic.getparamInit();
  $scope.failedTrade =FailedStatics.failedTrade;
  //查询接入渠道
  // $scope.channels =getChannel;
  //机构查询
  $scope.agencys=getAgency;
  /**
   *  时间默认初始化
   *  设置账期默认为年，初始化开始时间和结束时间之间的距离为1年
   *  账期为月的时候:
   *  默认查询一个月的时间：从开始这个月到上一个月
   *  账期为日的时候
   *  默认为7天的时间： 从开始时间到结束时间
   */
  $scope.param=FailedStatics.param;
  $scope.yearitem=FailedStatics.yearitem;

  /**
   * 返回码模糊查询下拉框
   */
  $scope.$watch('param.timeType', function(value){
    FailedLogic.timedefault(value);
  });
  $scope.conditionSW = false;
  $scope.disabledSw = false;
  $scope.getBL = function(businessLineCode){
    var bl = {
      businessLine:[]
    };
    if(businessLineCode){
      $timeout(function(){
        if(businessLineCode === '2'){
          $scope.conditionSW = true;
        }else {
          $scope.conditionSW = false;
        }
        $scope.disabledSw = true;
        bl.businessLine.push(businessLineCode);
        contrastiveAnalysis.getChannel(bl,function(res){
          if(res.data.length === 0){
            $scope.conditionSW = false;
          }else {
            $scope.channels = res.data;
            $scope.conditionSW = true;
          }
        });
      });
    }else {
      $scope.conditionSW = false;
      $scope.disabledSw = false;
    }
    $scope.failedTrade.transaction=[];
    $scope.failedTrade.agency=[];
    $scope.failedTrade.channel=[];
  };
   $scope.$watch('failedTrade.businessLine',function(value){
     $scope.transactions=failedTradServer.failedtransacTion().failedTrade;
   });
  /**
  * [timeFormart 时间格式化]
  * @param  {[type]} data [发送给后台的时间]
  * @return {[type]}      [格式化的数据]
  */
  function timeFormart(){
    if($scope.failedTrade.queryType===false){
      $scope.param.returnCode= is.array($scope.failedTrade.returnCode) && $scope.failedTrade.returnCode.length > 0 ?
      $scope.failedTrade.returnCode.map(function(item) {return   item.returnCode;})  : ['2A','3A','4A','5A',''];
      $scope.param.queryType='0';
    }else{
      $scope.param.returnCode=$scope.params.returnCode==='' ? [''] :  $scope.params.returnCode.split(',');
      $scope.param.queryType='1';
    }
    $scope.param.businessLine=[];
    $scope.failedTrade.businessLine ? $scope.param.businessLine.push($scope.failedTrade.businessLine.businessLineCode) : $scope.param.businessLine=[];
    $scope.param.provinceCode = contrastiveLogicServer.parseObj($scope.failedTrade.province,'provinceCode');
    $scope.param.organization = contrastiveLogicServer.parseObj($scope.failedTrade.agency,'organizationCode');
    $scope.param.transactionTypeCodes = contrastiveLogicServer.parsefailed($scope.failedTrade.transaction,'tradeTypeCode',$scope.transactions.getTransactions);
    $scope.param.transactionTypeNames = is.array($scope.failedTrade.transaction) && $scope.failedTrade.transaction.length > 0 ?
    $scope.failedTrade.transaction.map(function(item) {return item.tradeTypeName;}) : ['全部'];
    $scope.param.channel = contrastiveLogicServer.parseObj($scope.failedTrade.channel,'channelCode');
    if($scope.param.timeType==='4'){
        $scope.param.startTime=contrastiveLogicServer.timeSplitRequest($scope.yearitem.startTime).timeYear;
        $scope.param.startQuarter=contrastiveLogicServer.timeSplitRequest($scope.yearitem.startTime).quarter;
        $scope.param.endTime=contrastiveLogicServer.timeSplitRequest($scope.yearitem.endTime).timeYear;
        $scope.param.endQuarter=contrastiveLogicServer.timeSplitRequest($scope.yearitem.endTime).quarter;
    }else{
       $scope.param.startTime=FailedLogic.timeFormart($scope.param.timeType,$scope.yearitem.startTime);
       $scope.param.endTime=FailedLogic.timeFormart($scope.param.timeType,$scope.yearitem.endTime);
     }
  }
  /**
   *  页面查询DIV下拉控制方法 设置进入的时候为打开查询的样式 当添加符合时点击查询按钮
   *  利用广播把表单数据传给表单控制器查询数据
   */
    $scope.flex=false;
    $scope.shoumun = 1;
  //季度信息广播
    $scope.$on('formInvalid',function(event,data){
     $scope.butDisabled=data;
  });

  $scope.dropDrowSw = function() {
    if(Boolean($scope.shoumun)) {
      $scope.$on('queryDivSw',function(a,b){
        $scope.shoumun=b;
        if($scope.shoumun===0){
          $scope.flex=true;
        }else {
          $scope.flex = false;
        }
      });
        timeFormart($scope.yearitem);
         $scope._loading.show = true;
        $scope.$broadcast('tableitem', $scope.param);
    }else {
      $scope.shoumun = 1;
    }
  };
}
//依赖注入

failedTradeinput.$inject=['$scope','contrastiveAnalysis','$timeout','$filter','$mdDialog','CoreService',
                          'COREDATA','FailedStatics','contrastiveLogicServer','getChannel','getAgency','failedTradServer','FailedLogic'];
/**
 * [failedTradeTable 表格控制器]
 * @param  {[type]} COREDATA         [公共服务]
 * @param  {[type]} failedTradServer [失败交易服务]
 * @param  {[type]} CoreService      [公共服务]
 * @param  {[type]} $filter          [过滤器]
 * @return {[type]}                  [给表格显示]
 */
function failedTradeTable($scope, COREDATA, failedTradServer, CoreService,$filter,FailedLogic,PublicFn) {
 /**
  * [excelExport 导出路径对象]
  * @type {[String]}
  */
  $scope.excelExport = COREDATA.FORMPREFIX + 'OMdisplay/upayQM/businessQM/failureDealsExcel';
  /**
   * [description]
   * @param  {[type]} e      [广播参数]
   * @param  {[type]} value) {       queryfailureTrade(value);    } [把下拉数据对象放入获取表格数据方法内]
   * @return {[type]}        [表格数据]
   */
  $scope.$on('tableitem', function(e, value) {
    queryfailureTrade(value);
  });
  $scope.pageparam ={};
  $scope.page = {currentPage: 1,pageSize: '10'};
  $scope.pageparam.page = $scope.page;
 /**
  * [queryfailureTrade 表格查询方法,发送请求获取数据,$scope.seriesstateName表格标题数据,调用quarter方法,获取季度
  *                    当有数据的时候利用广播名字为'queryDivSw',给控制查询DIV隐藏，当没有数据的时候DIV就不隐藏分
  *                    别给$scope.item,和$scope.items对象赋值,2个表格对象,这里用2个对象接受同一个数据,其实避免分
  *                    页查询的时候互相干扰.给2个表格的当前页试着为1,页数为10.]
  * @param  {[type]} item [查询参数]
  * @return {[type]}      [如果查询成功返回判断有误数据,没有数据提示,查询不成功返回错误代码]
  */
  $scope.pageSW=false;
  function queryfailureTrade(item) {
  FailedLogic.timeFormart();
  item.page={currentPage: 1,pageSize: '10'};
  failedTradServer.queryfailureTradeStatistics(item, function(result) {
      if (result.state === '10000'){
        $scope._loading.show = false;
        if(result.data.sumData.FailedDeals.length>0){
          $scope.collectSw=$scope.conditionSW===false ? false : true;      //业务线表格显示渠道,机构,汇总开关
          $scope.pageSW=true;
          $scope.seriesstateName = PublicFn.fmtDateTitle(item,'timeType')+' 失败笔数分析表';
          $scope.$emit('queryDivSw',0);
          exportList(item);
          $scope.itemss=result.data;
          $scope.sumSumData=result.data.sumData.sum;
          $scope.totalCounts = result.data.sumData.page.totalCount;
        }else{
          $scope._loading.show = false;
          $scope.pageSW=false;
          $scope.$emit('queryDivSw',1);
          CoreService.dialog(result.state);
         }
      }else {
        $scope._loading.show = false;
        $scope.pageSW=false;
        CoreService.dialog(result.state);
        $scope.$emit('queryDivSw',1);
      }
    });
  }
  function  exportList(item) {
    $scope.pageparam={};
    $scope.ExportList={};
     angular.copy(item, $scope.ExportList);
     angular.copy(item,$scope.pageparam);
     $scope.ExportList.title=$scope.seriesstateName;
  }
/**
 * [turnToPage 合表分页方法,第一个表查询,所有给标示赋值为1]
 * @param  {[Object]} param [查询所需要的参数]
 * @return {[Object]}       [返回表格数据]
 */
  $scope.sumturnToPage=function(params){
    $scope._loading.show = true;
    failedTradServer.queryfailureTradeStatistics(params, function(result) {
    $scope._loading.show = false;
    $scope.itemss=result.data;
    $scope.totalCounts = result.data.sumData.page.totalCount;
    $scope.sumSumData=result.data.sumData.sum;
    });
 };
}
//依赖注入
failedTradeTable.$inject=['$scope','COREDATA','failedTradServer','CoreService','$filter','FailedLogic','PublicFn'];
})();